package lily.phs.urh;

import lily.phs.object.HealthTest;
import lily.phs.object.MedicateRecord;
import lily.phs.object.NonimmuneVaccineHistory;
import lily.phs.urh.R;
import lily.ui.control.MyPopupWindow;


import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.InputType;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;

/**
 * 健康体检表（Page 6）的界面布局和数据处理封装的view类
 * @author heckassitant
 *
 */
public class HealthTest_6 extends LinearLayout implements OnClickListener, OnFocusChangeListener{

	private static final String[] obedience = {"1.规律","2.间断","3.不服药"};
	private MyPopupWindow mPopupWindow;
	private Context mContext;
	private int profileid = 0;
	private int testid = 0;
	
	private EditText et_medicine_name_1,et_medicine_name_2,et_medicine_name_3,et_medicine_name_4,et_medicine_name_5,et_medicine_name_6,
	                 et_medicine_usage_1,et_medicine_usage_2,et_medicine_usage_3,et_medicine_usage_4,et_medicine_usage_5,et_medicine_usage_6,
	                 et_medicine_dosage_1,et_medicine_dosage_2,et_medicine_dosage_3,et_medicine_dosage_4,et_medicine_dosage_5,et_medicine_dosage_6,
	                 et_medicine_time_1,et_medicine_time_2,et_medicine_time_3,et_medicine_time_4,et_medicine_time_5,et_medicine_time_6,
	                 et_medicine_obedience_1,et_medicine_obedience_2,et_medicine_obedience_3,et_medicine_obedience_4,et_medicine_obedience_5,
	                 et_medicine_obedience_6,et_vaccine_name_1,et_vaccine_name_2,et_vaccine_name_3,et_vaccine_date_1,et_vaccine_date_2,
	                 et_vaccine_date_3,et_vaccine_hospital_1,et_vaccine_hospital_2,et_vaccine_hospital_3,et_healthtest_abnormal_1,
	                 et_healthtest_abnormal_2,et_healthtest_abnormal_3,et_healthtest_abnormal_4,et_lose_weight_goal,et_advical_vaccine,
	                 et_hazards_control_other;
	private RadioGroup rgHealthTestAbnormal;
	private RadioButton rb_healthtest_abnormal_no,rb_healthtest_abnormal_yes;
	
	private CheckBox cb_healthguide_1,cb_healthguide_2,cb_healthguide_3,cb_hazards_control_1,cb_hazards_control_2,cb_hazards_control_3,
	                 cb_hazards_control_4,cb_hazards_control_5,cb_hazards_control_6,cb_hazards_control_7;
	
	private MedicateRecord mr,mr1,mr2,mr3,mr4,mr5;
	
	private NonimmuneVaccineHistory nvh,nvh1,nvh2;
	private HealthTest ht;
	private View child;
	
	public HealthTest_6(Context context,int profileId,int testId) {
		super(context);
		// TODO Auto-generated constructor stub
		//档案编号
		this.profileid = profileId;
		this.testid = testId;
		this.mContext = context;
		
		final LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		child = inflater.inflate(R.layout.healthtest_6, null);
		
		//findViewsById();
		//initViews();
	}
	
	public int getProfileid() {
		return profileid;
	}

	public void setProfileid(int profileid) {
		this.profileid = profileid;
	}

	public int getTestid() {
		return testid;
	}

	public void setTestid(int testid) {
		this.testid = testid;
	}

	public void findViewsById() {
		// TODO Auto-generated method stub
		
		et_medicine_name_1 = (EditText)child.findViewById(R.id.ht6_medicine_name_1);
		et_medicine_name_2 = (EditText)child.findViewById(R.id.ht6_medicine_name_2);
		et_medicine_name_3 = (EditText)child.findViewById(R.id.ht6_medicine_name_3);
		et_medicine_name_4 = (EditText)child.findViewById(R.id.ht6_medicine_name_4);
		et_medicine_name_5 = (EditText)child.findViewById(R.id.ht6_medicine_name_5);
		et_medicine_name_6 = (EditText)child.findViewById(R.id.ht6_medicine_name_6);
        et_medicine_usage_1 = (EditText)child.findViewById(R.id.ht6_medicine_usage_1);
        et_medicine_usage_2 = (EditText)child.findViewById(R.id.ht6_medicine_usage_2);
        et_medicine_usage_3 = (EditText)child.findViewById(R.id.ht6_medicine_usage_3);
        et_medicine_usage_4 = (EditText)child.findViewById(R.id.ht6_medicine_usage_4);
        et_medicine_usage_5 = (EditText)child.findViewById(R.id.ht6_medicine_usage_5);
        et_medicine_usage_6 = (EditText)child.findViewById(R.id.ht6_medicine_usage_6);
        et_medicine_dosage_1 = (EditText)child.findViewById(R.id.ht6_medicine_dosage_1);
        et_medicine_dosage_2 = (EditText)child.findViewById(R.id.ht6_medicine_dosage_2);
        et_medicine_dosage_3 = (EditText)child.findViewById(R.id.ht6_medicine_dosage_3);
        et_medicine_dosage_4 = (EditText)child.findViewById(R.id.ht6_medicine_dosage_4);
        et_medicine_dosage_5 = (EditText)child.findViewById(R.id.ht6_medicine_dosage_5);
        et_medicine_dosage_6 = (EditText)child.findViewById(R.id.ht6_medicine_dosage_6);
        et_medicine_time_1 = (EditText)child.findViewById(R.id.ht6_medicine_time_1);
        et_medicine_time_2 = (EditText)child.findViewById(R.id.ht6_medicine_time_2);
        et_medicine_time_3 = (EditText)child.findViewById(R.id.ht6_medicine_time_3);
        et_medicine_time_4 = (EditText)child.findViewById(R.id.ht6_medicine_time_4);
        et_medicine_time_5 = (EditText)child.findViewById(R.id.ht6_medicine_time_5);
        et_medicine_time_6 = (EditText)child.findViewById(R.id.ht6_medicine_time_6);
        et_medicine_obedience_1 = (EditText)child.findViewById(R.id.ht6_medicine_obedience_1);
        et_medicine_obedience_1.setInputType(InputType.TYPE_NULL);//不显示虚拟键盘
        et_medicine_obedience_1.setOnFocusChangeListener(this);
        et_medicine_obedience_1.setOnClickListener(this);
        et_medicine_obedience_2 = (EditText)child.findViewById(R.id.ht6_medicine_obedience_2);
        et_medicine_obedience_2.setInputType(InputType.TYPE_NULL);//不显示虚拟键盘
        et_medicine_obedience_2.setOnFocusChangeListener(this);
        et_medicine_obedience_2.setOnClickListener(this);
        et_medicine_obedience_3 = (EditText)child.findViewById(R.id.ht6_medicine_obedience_3);
        et_medicine_obedience_3.setInputType(InputType.TYPE_NULL);//不显示虚拟键盘
        et_medicine_obedience_3.setOnFocusChangeListener(this);
        et_medicine_obedience_3.setOnClickListener(this);
        et_medicine_obedience_4 = (EditText)child.findViewById(R.id.ht6_medicine_obedience_4);
        et_medicine_obedience_4.setInputType(InputType.TYPE_NULL);//不显示虚拟键盘
        et_medicine_obedience_4.setOnFocusChangeListener(this);
        et_medicine_obedience_4.setOnClickListener(this);
        et_medicine_obedience_5 = (EditText)child.findViewById(R.id.ht6_medicine_obedience_5);
        et_medicine_obedience_5.setInputType(InputType.TYPE_NULL);//不显示虚拟键盘
        et_medicine_obedience_5.setOnFocusChangeListener(this);
        et_medicine_obedience_5.setOnClickListener(this);
        et_medicine_obedience_6 = (EditText)child.findViewById(R.id.ht6_medicine_obedience_6);
        et_medicine_obedience_6.setInputType(InputType.TYPE_NULL);//不显示虚拟键盘
        et_medicine_obedience_6.setOnFocusChangeListener(this);
        et_medicine_obedience_6.setOnClickListener(this);
        et_vaccine_name_1 = (EditText)child.findViewById(R.id.ht6_vaccine_name_1);
        et_vaccine_name_2 = (EditText)child.findViewById(R.id.ht6_vaccine_name_2);
        et_vaccine_name_3 = (EditText)child.findViewById(R.id.ht6_vaccine_name_3);
        et_vaccine_date_1 = (EditText)child.findViewById(R.id.ht6_vaccine_date_1);
        et_vaccine_date_1.setInputType(InputType.TYPE_NULL);//不显示虚拟键盘
        et_vaccine_date_1.setOnClickListener(this);
        et_vaccine_date_1.setOnFocusChangeListener(this);
        et_vaccine_date_2 = (EditText)child.findViewById(R.id.ht6_vaccine_date_2);
        et_vaccine_date_2.setInputType(InputType.TYPE_NULL);//不显示虚拟键盘
        et_vaccine_date_2.setOnClickListener(this);
        et_vaccine_date_2.setOnFocusChangeListener(this);
        et_vaccine_date_3 = (EditText)child.findViewById(R.id.ht6_vaccine_date_3);
        et_vaccine_date_3.setInputType(InputType.TYPE_NULL);//不显示虚拟键盘
        et_vaccine_date_3.setOnClickListener(this);
        et_vaccine_date_3.setOnFocusChangeListener(this);
        et_vaccine_hospital_1 = (EditText)child.findViewById(R.id.ht6_vaccine_hospital_1);
        et_vaccine_hospital_2 = (EditText)child.findViewById(R.id.ht6_vaccine_hospital_2);
        et_vaccine_hospital_3 = (EditText)child.findViewById(R.id.ht6_vaccine_hospital_3);
        et_healthtest_abnormal_1 = (EditText)child.findViewById(R.id.ht6_healthtest_abnormal_1);
        et_healthtest_abnormal_1.setOnFocusChangeListener(this);
        et_healthtest_abnormal_1.setOnClickListener(this);
        et_healthtest_abnormal_2 = (EditText)child.findViewById(R.id.ht6_healthtest_abnormal_2);
        et_healthtest_abnormal_3 = (EditText)child.findViewById(R.id.ht6_healthtest_abnormal_3);
        et_healthtest_abnormal_4 = (EditText)child.findViewById(R.id.ht6_healthtest_abnormal_4);
        et_lose_weight_goal = (EditText)child.findViewById(R.id.ht6_lose_weight_goal);
        et_lose_weight_goal.setOnFocusChangeListener(this);
        et_lose_weight_goal.setOnClickListener(this);
        et_advical_vaccine = (EditText)child.findViewById(R.id.ht6_advical_vaccine);
        et_advical_vaccine.setOnFocusChangeListener(this);
        et_advical_vaccine.setOnClickListener(this);
        et_hazards_control_other = (EditText)child.findViewById(R.id.ht6_hazards_control_other);
        et_hazards_control_other.setOnFocusChangeListener(this);
        et_hazards_control_other.setOnClickListener(this);
        rb_healthtest_abnormal_no = (RadioButton)child.findViewById(R.id.ht6_healthtest_abnormal_no);
		rb_healthtest_abnormal_yes = (RadioButton)child.findViewById(R.id.ht6_healthtest_abnormal_yes);
		cb_healthguide_1 = (CheckBox)child.findViewById(R.id.ht6_healthguide_1);
		cb_healthguide_2 = (CheckBox)child.findViewById(R.id.ht6_healthguide_2);
		cb_healthguide_3 = (CheckBox)child.findViewById(R.id.ht6_healthguide_3);
		cb_hazards_control_1 = (CheckBox)child.findViewById(R.id.ht6_hazards_control_1);
		cb_hazards_control_2 = (CheckBox)child.findViewById(R.id.ht6_hazards_control_2);
		cb_hazards_control_3 = (CheckBox)child.findViewById(R.id.ht6_hazards_control_3);
        cb_hazards_control_4 = (CheckBox)child.findViewById(R.id.ht6_hazards_control_4);
        cb_hazards_control_5 = (CheckBox)child.findViewById(R.id.ht6_hazards_control_5);
        cb_hazards_control_6 = (CheckBox)child.findViewById(R.id.ht6_hazards_control_6);
        cb_hazards_control_7 = (CheckBox)child.findViewById(R.id.ht6_hazards_control_7);
        rgHealthTestAbnormal = (RadioGroup)child.findViewById(R.id.healthtestabnormal);
        
	}
	
	/**
	 * 根据ID从数据库中读出数据并填充views
	 */
	public void initViews() {
		// TODO Auto-generated method stub
		
		initialState();
		
		String temp = "";
		int itemp = -1;
		
		//吃药记录
		//MedicateRecord mr = new MedicateRecord(mContext);
		//mr.selectMedicateRecord(profileid, testid, 1);
		temp = mr.getMere_name();
		et_medicine_name_1.setText(temp);
		temp = mr.getMere_usage();
		et_medicine_usage_1.setText(temp);
		temp = mr.getMere_dosage();
		et_medicine_dosage_1.setText(temp);
		temp = mr.getMere_time();
		et_medicine_time_1.setText(temp);
		itemp = mr.getMere_obedience();
		if(itemp==1||itemp==2||itemp==3){
			et_medicine_obedience_1.setText(String.valueOf(itemp));
		}
		
		//mr.selectMedicateRecord(profileid, testid, 2);
		temp = mr1.getMere_name();
		et_medicine_name_2.setText(temp);
		temp = mr1.getMere_usage();
		et_medicine_usage_2.setText(temp);
		temp = mr1.getMere_dosage();
		et_medicine_dosage_2.setText(temp);
		temp = mr1.getMere_time();
		et_medicine_time_2.setText(temp);
		itemp = mr1.getMere_obedience();
		if(itemp==1||itemp==2||itemp==3){
			et_medicine_obedience_2.setText(String.valueOf(itemp));
		}
		
		//mr.selectMedicateRecord(profileid, testid, 3);
		temp = mr2.getMere_name();
		et_medicine_name_3.setText(temp);
		temp = mr2.getMere_usage();
		et_medicine_usage_3.setText(temp);
		temp = mr2.getMere_dosage();
		et_medicine_dosage_3.setText(temp);
		temp = mr2.getMere_time();
		et_medicine_time_3.setText(temp);
		itemp = mr2.getMere_obedience();
		if(itemp==1||itemp==2||itemp==3){
			et_medicine_obedience_3.setText(String.valueOf(itemp));
		}
		
		//mr.selectMedicateRecord(profileid, testid, 4);
		temp = mr3.getMere_name();
		et_medicine_name_4.setText(temp);
		temp = mr3.getMere_usage();
		et_medicine_usage_4.setText(temp);
		temp = mr3.getMere_dosage();
		et_medicine_dosage_4.setText(temp);
		temp = mr3.getMere_time();
		et_medicine_time_4.setText(temp);
		itemp = mr3.getMere_obedience();
		if(itemp==1||itemp==2||itemp==3){
			et_medicine_obedience_4.setText(String.valueOf(itemp));
		}
		
		//mr.selectMedicateRecord(profileid, testid, 5);
		temp = mr4.getMere_name();
		et_medicine_name_5.setText(temp);
		temp = mr4.getMere_usage();
		et_medicine_usage_5.setText(temp);
		temp = mr4.getMere_dosage();
		et_medicine_dosage_5.setText(temp);
		temp = mr4.getMere_time();
		et_medicine_time_5.setText(temp);
		itemp = mr4.getMere_obedience();
		if(itemp==1||itemp==2||itemp==3){
			et_medicine_obedience_5.setText(String.valueOf(itemp));
		}
		
		//mr.selectMedicateRecord(profileid, testid, 6);
		temp = mr5.getMere_name();
		et_medicine_name_6.setText(temp);
		temp = mr5.getMere_usage();
		et_medicine_usage_6.setText(temp);
		temp = mr5.getMere_dosage();
		et_medicine_dosage_6.setText(temp);
		temp = mr5.getMere_time();
		et_medicine_time_6.setText(temp);
		itemp = mr5.getMere_obedience();
		if(itemp==1||itemp==2||itemp==3){
			et_medicine_obedience_6.setText(String.valueOf(itemp));
		}
		
		//非免疫预防接种
		//NonimmuneVaccineHistory nvh = new NonimmuneVaccineHistory(mContext);
		
		//nvh.selectNonimmuneVaccineHistory(profileid, testid, 1);
		temp = nvh.getNovh_name();
		et_vaccine_name_1.setText(temp);
		temp = nvh.getNovh_date();
		et_vaccine_date_1.setText(temp);
		temp = nvh.getNovh_hospital();
		et_vaccine_hospital_1.setText(temp);
		//nvh.selectNonimmuneVaccineHistory(profileid, testid, 2);
		
		temp = nvh1.getNovh_name();
		et_vaccine_name_2.setText(temp);
		temp = nvh1.getNovh_date();
		et_vaccine_date_2.setText(temp);
		temp = nvh1.getNovh_hospital();
		et_vaccine_hospital_2.setText(temp);
		//nvh.selectNonimmuneVaccineHistory(profileid, testid, 3);
		
		temp = nvh2.getNovh_name();
		et_vaccine_name_3.setText(temp);
		temp = nvh2.getNovh_date();
		et_vaccine_date_3.setText(temp);
		temp = nvh2.getNovh_hospital();
		et_vaccine_hospital_3.setText(temp);
		
		//体检page6
		//HealthTest ht = new HealthTest(mContext);
		itemp = ht.getHete_ishealthtest_normal();
		switch(itemp){
		case 1:
			rgHealthTestAbnormal.check(0);
			rb_healthtest_abnormal_no.setChecked(true);
			break;
		case 2:{
			rgHealthTestAbnormal.check(1);
			rb_healthtest_abnormal_yes.setChecked(true);
			temp = ht.getHete_healthtest_abnormal(); 
			if(temp.length()>0){
				String[] ss = temp.split(",");
				for(int i=0;i<ss.length;i++){
					switch(i){
					case 0:
						et_healthtest_abnormal_1.setText(ss[i]);
						break;
					case 1:
						et_healthtest_abnormal_2.setText(ss[i]);
						break;
					case 2:
						et_healthtest_abnormal_3.setText(ss[i]);
						break;
					case 3:
						et_healthtest_abnormal_4.setText(ss[i]);
						break;
					}
				}
			}
		}
		}
		
		temp = ht.getHete_healthguide();
		if(temp.length()>0){
			String[] ss = temp.split(",");
			for(int i=0;i<ss.length;i++){
				int k = Integer.parseInt(ss[i]);
				switch(k){
				case 1:
					cb_healthguide_1.setChecked(true);
					break;
				case 2:
					cb_healthguide_2.setChecked(true);
					break;
				case 3:
					cb_healthguide_3.setChecked(true);
					break;
				}
			}
		}
		
		temp = ht.getHete_hazard_control();
		if(temp.length()>0){
			String[] ss = temp.split(",");
			for(int i=0;i<ss.length;i++){
				int k = Integer.parseInt(ss[i]);
				switch(k){
				case 1:
					cb_hazards_control_1.setChecked(true);
					break;
				case 2:
					cb_hazards_control_2.setChecked(true);
					break;
				case 3:
					cb_hazards_control_3.setChecked(true);
					break;
				case 4:
					cb_hazards_control_4.setChecked(true);
					break;
				case 5:{
					cb_hazards_control_5.setChecked(true);
					temp = ht.getHete_goal_of_lose_weight();
					et_lose_weight_goal.setText(temp);
					break;
				}
				case 6:{
					cb_hazards_control_6.setChecked(true);
					temp = ht.getHete_advical_vaccine();
					et_advical_vaccine.setText(temp);
					break;
				}
				case 7:{
					cb_hazards_control_7.setChecked(true);
					temp = ht.getHete_other_hazard();
					et_hazards_control_other.setText(temp);
					break;
				}
				}
			}
		}
		
		
	}
	
	/**
	 * 初始时为空表：横线为空，所有选择项未选中
	 */
	private void initialState() {
		// TODO Auto-generated method stub
		et_medicine_name_1.setText("");
		et_medicine_name_2.setText("");
		et_medicine_name_3.setText("");
		et_medicine_name_4.setText("");
		et_medicine_name_5.setText("");
		et_medicine_name_6.setText("");
        et_medicine_usage_1.setText("");
        et_medicine_usage_2.setText("");
        et_medicine_usage_3.setText("");
        et_medicine_usage_4.setText("");
        et_medicine_usage_5.setText("");
        et_medicine_usage_6.setText("");
        et_medicine_dosage_1.setText("");
        et_medicine_dosage_2.setText("");
        et_medicine_dosage_3.setText("");
        et_medicine_dosage_4.setText("");
        et_medicine_dosage_5.setText("");
        et_medicine_dosage_6.setText("");
        et_medicine_time_1.setText("");
        et_medicine_time_2.setText("");
        et_medicine_time_3.setText("");
        et_medicine_time_4.setText("");
        et_medicine_time_5.setText("");
        et_medicine_time_6.setText("");
        et_medicine_obedience_1.setText("");
        et_medicine_obedience_2.setText("");
        et_medicine_obedience_3.setText("");
        et_medicine_obedience_4.setText("");
        et_medicine_obedience_5.setText("");
        et_medicine_obedience_6.setText("");
        et_vaccine_name_1.setText("");
        et_vaccine_name_2.setText("");
        et_vaccine_name_3.setText("");
        et_vaccine_date_1.setText("");
        et_vaccine_date_2.setText("");
        et_vaccine_date_3.setText("");
        et_vaccine_hospital_1.setText("");
        et_vaccine_hospital_2.setText("");
        et_vaccine_hospital_3.setText("");
        et_healthtest_abnormal_1.setText("");
        et_healthtest_abnormal_2.setText("");
        et_healthtest_abnormal_3.setText("");
        et_healthtest_abnormal_4.setText("");
        et_lose_weight_goal.setText("");
        et_advical_vaccine.setText("");
        et_hazards_control_other.setText("");
		rb_healthtest_abnormal_no.setChecked(false);
		rb_healthtest_abnormal_yes.setChecked(false);
		cb_healthguide_1.setChecked(false);
		cb_healthguide_2.setChecked(false);
		cb_healthguide_3.setChecked(false);
		cb_hazards_control_1.setChecked(false);
		cb_hazards_control_2.setChecked(false);
		cb_hazards_control_3.setChecked(false);
        cb_hazards_control_4.setChecked(false);
        cb_hazards_control_5.setChecked(false);
        cb_hazards_control_6.setChecked(false);
        cb_hazards_control_7.setChecked(false);
	}

	/**
	 * 添加view到contentview
	 */
	public void setMyView(){
		this.removeAllViews();
		LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		this.addView(child,lp);
		
	}
	
	/**
	 * 将整个界面作为一个view对象
	 * @return 整个界面view
	 */
	public View getMyView(){
		return this.child;
	}
	
	/**
	 * 检出数据
	 */
	public void fetchData(){
        mr = new MedicateRecord(mContext);
		mr.selectMedicateRecord(profileid, testid, 1);
		
		mr1 = new MedicateRecord(mContext);
		mr1.selectMedicateRecord(profileid, testid, 2);
		
		mr2 = new MedicateRecord(mContext);
		mr2.selectMedicateRecord(profileid, testid, 3);
		
		mr3 = new MedicateRecord(mContext);
		mr3.selectMedicateRecord(profileid, testid, 4);
		
		mr4 = new MedicateRecord(mContext);
		mr4.selectMedicateRecord(profileid, testid, 5);
		
		mr5 = new MedicateRecord(mContext);
		mr5.selectMedicateRecord(profileid, testid, 6);
		
		nvh = new NonimmuneVaccineHistory(mContext);
		nvh.selectNonimmuneVaccineHistory(profileid, testid, 1);
		
		nvh1 = new NonimmuneVaccineHistory(mContext);
		nvh1.selectNonimmuneVaccineHistory(profileid, testid, 2);
		
		nvh2 = new NonimmuneVaccineHistory(mContext);
		nvh2.selectNonimmuneVaccineHistory(profileid, testid, 3);
		
		ht = new HealthTest(mContext);
		ht.selectHealthTest(profileid, testid);
		
	}
	
	/**
	 * 读取views的值并保存至ID对应的记录中
	 */
	public void saveViews() {
		String temp="";
		StringBuffer sb;
//		int itemp=0;
		
		//吃药记录
		MedicateRecord mr = new MedicateRecord(mContext);
		mr.setMere_profileid(profileid);
		mr.setMere_testid(testid);
		
		mr.setMere_id(1);
		temp = et_medicine_name_1.getText().toString();
		mr.setMere_name(temp);
		temp = et_medicine_usage_1.getText().toString();
		mr.setMere_usage(temp);
		temp = et_medicine_dosage_1.getText().toString();
		mr.setMere_dosage(temp);
		temp = et_medicine_time_1.getText().toString();
		mr.setMere_time(temp);
		temp = et_medicine_obedience_1.getText().toString();
		if(temp.length()>0){
			mr.setMere_obedience(Integer.parseInt(temp));
		}
		mr.updateMedicateRecord(profileid, testid, 1);
		mr.setMere_id(2);
		temp = et_medicine_name_2.getText().toString();
		mr.setMere_name(temp);
		temp = et_medicine_usage_2.getText().toString();
		mr.setMere_usage(temp);
		temp = et_medicine_dosage_2.getText().toString();
		mr.setMere_dosage(temp);
		temp = et_medicine_time_2.getText().toString();
		mr.setMere_time(temp);
		temp = et_medicine_obedience_2.getText().toString();
		if(temp.length()>0){
			mr.setMere_obedience(Integer.parseInt(temp));
		}
		mr.updateMedicateRecord(profileid, testid, 2);
		mr.setMere_id(3);
		temp = et_medicine_name_3.getText().toString();
		mr.setMere_name(temp);
		temp = et_medicine_usage_3.getText().toString();
		mr.setMere_usage(temp);
		temp = et_medicine_dosage_3.getText().toString();
		mr.setMere_dosage(temp);
		temp = et_medicine_time_3.getText().toString();
		mr.setMere_time(temp);
		temp = et_medicine_obedience_3.getText().toString();
		if(temp.length()>0){
			mr.setMere_obedience(Integer.parseInt(temp));
		}
		mr.updateMedicateRecord(profileid, testid, 3);
		mr.setMere_id(4);
		temp = et_medicine_name_4.getText().toString();
		mr.setMere_name(temp);
		temp = et_medicine_usage_4.getText().toString();
		mr.setMere_usage(temp);
		temp = et_medicine_dosage_4.getText().toString();
		mr.setMere_dosage(temp);
		temp = et_medicine_time_4.getText().toString();
		mr.setMere_time(temp);
		temp = et_medicine_obedience_4.getText().toString();
		if(temp.length()>0){
			mr.setMere_obedience(Integer.parseInt(temp));
		}
		mr.updateMedicateRecord(profileid, testid, 4);
		mr.setMere_id(5);
		temp = et_medicine_name_5.getText().toString();
		mr.setMere_name(temp);
		temp = et_medicine_usage_5.getText().toString();
		mr.setMere_usage(temp);
		temp = et_medicine_dosage_5.getText().toString();
		mr.setMere_dosage(temp);
		temp = et_medicine_time_5.getText().toString();
		mr.setMere_time(temp);
		temp = et_medicine_obedience_5.getText().toString();
		if(temp.length()>0){
			mr.setMere_obedience(Integer.parseInt(temp));
		}
		mr.updateMedicateRecord(profileid, testid, 5);
		mr.setMere_id(6);
		temp = et_medicine_name_6.getText().toString();
		mr.setMere_name(temp);
		temp = et_medicine_usage_6.getText().toString();
		mr.setMere_usage(temp);
		temp = et_medicine_dosage_6.getText().toString();
		mr.setMere_dosage(temp);
		temp = et_medicine_time_6.getText().toString();
		mr.setMere_time(temp);
		temp = et_medicine_obedience_6.getText().toString();
		if(temp.length()>0){
			mr.setMere_obedience(Integer.parseInt(temp));
		}
		mr.updateMedicateRecord(profileid, testid, 6);
		
		//预防接种
		NonimmuneVaccineHistory nvh = new NonimmuneVaccineHistory(mContext);
		nvh.setNovh_profileid(profileid);
		nvh.setNovh_testid(testid);
		
		nvh.setNovh_id(1);
		temp = et_vaccine_name_1.getText().toString();
		nvh.setNovh_name(temp);
		temp = et_vaccine_date_1.getText().toString();
		nvh.setNovh_date(temp);
		temp = et_vaccine_hospital_1.getText().toString();
		nvh.setNovh_hospital(temp);
		nvh.updateNonimmuneVaccineHistory(profileid, testid, 1);
		nvh.setNovh_id(2);
		temp = et_vaccine_name_2.getText().toString();
		nvh.setNovh_name(temp);
		temp = et_vaccine_date_2.getText().toString();
		nvh.setNovh_date(temp);
		temp = et_vaccine_hospital_2.getText().toString();
		nvh.setNovh_hospital(temp);
		nvh.updateNonimmuneVaccineHistory(profileid, testid, 2);
		nvh.setNovh_id(3);
		temp = et_vaccine_name_3.getText().toString();
		nvh.setNovh_name(temp);
		temp = et_vaccine_date_3.getText().toString();
		nvh.setNovh_date(temp);
		temp = et_vaccine_hospital_3.getText().toString();
		nvh.setNovh_hospital(temp);
		nvh.updateNonimmuneVaccineHistory(profileid, testid, 3);
		
		//
		HealthTest ht = new HealthTest(mContext);
		ht.setHete_profileid(profileid);
		ht.setHete_testid(testid);
		ht.selectHealthTest(profileid, testid);
		
		if(rb_healthtest_abnormal_no.isChecked()==true){
			ht.setHete_ishealthtest_normal(1);
		}else if(rb_healthtest_abnormal_yes.isChecked()==true){
			ht.setHete_ishealthtest_normal(2);
			sb = new StringBuffer();
			temp = et_healthtest_abnormal_1.getText().toString();
			sb.append(temp).append(",");
			temp = et_healthtest_abnormal_2.getText().toString();
			sb.append(temp).append(",");
			temp = et_healthtest_abnormal_3.getText().toString();
			sb.append(temp).append(",");
			temp = et_healthtest_abnormal_4.getText().toString();
			sb.append(temp).append(",");
			ht.setHete_healthtest_abnormal(sb.toString());
		}
		
		sb = new StringBuffer();
		if(cb_healthguide_1.isChecked()==true){
			sb.append("1,");
		}
		if(cb_healthguide_2.isChecked()==true){
			sb.append("2,");
		}
		if(cb_healthguide_3.isChecked()==true){
			sb.append("3,");
		}
		ht.setHete_healthguide(sb.toString());
		
		sb = new StringBuffer();
		if(cb_hazards_control_1.isChecked()==true){
			sb.append("1,");
		}
		if(cb_hazards_control_2.isChecked()==true){
			sb.append("2,");
		}
		if(cb_hazards_control_3.isChecked()==true){
			sb.append("3,");
		}
		if(cb_hazards_control_4.isChecked()==true){
			sb.append("4,");
		}
		if(cb_hazards_control_5.isChecked()==true){
			sb.append("5,");
			temp = et_lose_weight_goal.getText().toString();
			ht.setHete_goal_of_lose_weight(temp);
		}
		if(cb_hazards_control_6.isChecked()==true){
			sb.append("6,");
			temp = et_advical_vaccine.getText().toString();
			ht.setHete_advical_vaccine(temp);
		}
		if(cb_hazards_control_7.isChecked()==true){
			sb.append("7,");
			temp = et_hazards_control_other.getText().toString();
			ht.setHete_other_hazard(temp);
		}
		ht.setHete_hazard_control(sb.toString());
		ht.updateHealthTest(profileid, testid);
		
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v==et_medicine_time_1){
			mPopupWindow = new MyPopupWindow(mContext);			
			mPopupWindow.showAtLocation(v.findViewById(R.id.ht6_medicine_time_1), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_medicine_time_1.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}
				
			});
		}
		else if(v==et_medicine_time_2){
			mPopupWindow = new MyPopupWindow(mContext);			
			mPopupWindow.showAtLocation(v.findViewById(R.id.ht6_medicine_time_2), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_medicine_time_2.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}
				
			});
		}
		else if(v==et_medicine_time_3){
			mPopupWindow = new MyPopupWindow(mContext);			
			mPopupWindow.showAtLocation(v.findViewById(R.id.ht6_medicine_time_3), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_medicine_time_3.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}
				
			});
		}
		else if(v==et_medicine_time_4){
			mPopupWindow = new MyPopupWindow(mContext);			
			mPopupWindow.showAtLocation(v.findViewById(R.id.ht6_medicine_time_4), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_medicine_time_4.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}
				
			});
		}
		else if(v==et_medicine_time_5){
			mPopupWindow = new MyPopupWindow(mContext);			
			mPopupWindow.showAtLocation(v.findViewById(R.id.ht6_medicine_time_5), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_medicine_time_5.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}
				
			});
		}
		else if(v==et_medicine_time_6){
			mPopupWindow = new MyPopupWindow(mContext);			
			mPopupWindow.showAtLocation(v.findViewById(R.id.ht6_medicine_time_6), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_medicine_time_6.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}
				
			});
		}
		else if(v==et_vaccine_date_1){
			mPopupWindow = new MyPopupWindow(mContext);			
			mPopupWindow.showAtLocation(v.findViewById(R.id.ht6_vaccine_date_1), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_vaccine_date_1.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}				
			});
		}
		else if(v==et_vaccine_date_2){
			mPopupWindow = new MyPopupWindow(mContext);			
			mPopupWindow.showAtLocation(v.findViewById(R.id.ht6_vaccine_date_2), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_vaccine_date_2.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}				
			});
		}
		else if(v==et_vaccine_date_3){
			mPopupWindow = new MyPopupWindow(mContext);			
			mPopupWindow.showAtLocation(v.findViewById(R.id.ht6_vaccine_date_3), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_vaccine_date_3.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}				
			});
		}
		else if(v==et_healthtest_abnormal_1){
			rb_healthtest_abnormal_no.setChecked(false);
			rb_healthtest_abnormal_yes.setChecked(true);
		}
		else if(v==et_lose_weight_goal){
			cb_hazards_control_5.setChecked(true);
		}
		else if(v==et_advical_vaccine){
			cb_hazards_control_6.setChecked(true);
		}
		else if(v==et_hazards_control_other){
			cb_hazards_control_7.setChecked(true);
		}
		else if(v==et_medicine_obedience_1){
			MyDialog(1).show();
		}
		else if(v==et_medicine_obedience_2){
			MyDialog(2).show();
		}
		else if(v==et_medicine_obedience_3){
			MyDialog(3).show();
		}
		else if(v==et_medicine_obedience_4){
			MyDialog(4).show();
		}
		else if(v==et_medicine_obedience_5){
			MyDialog(5).show();
		}
		else if(v==et_medicine_obedience_6){
			MyDialog(6).show();
		}
	}

	/**
	 * 服药依从性的选择框
	 */
	private Dialog MyDialog(int id) {
		// TODO Auto-generated method stub
		final int ID = id;
		return new AlertDialog.Builder(mContext)
		.setTitle("服药依从性")
		.setSingleChoiceItems(obedience, -1, new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				switch(ID){
				case 1:
					et_medicine_obedience_1.setText(String.valueOf(which+1));
					MyDialog(1).cancel();
					break;
				case 2:
					et_medicine_obedience_2.setText(String.valueOf(which+1));
					MyDialog(2).cancel();
					break;
				case 3:
					et_medicine_obedience_3.setText(String.valueOf(which+1));
					MyDialog(3).cancel();
					break;
				case 4:
					et_medicine_obedience_4.setText(String.valueOf(which+1));
					MyDialog(4).cancel();
					break;
				case 5:
					et_medicine_obedience_5.setText(String.valueOf(which+1));
					MyDialog(5).cancel();
					break;
				case 6:
					et_medicine_obedience_6.setText(String.valueOf(which+1));
					MyDialog(6).cancel();
					break;
				}
				dialog.dismiss();
			}
		})
		.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
			}
		})
		.create();		
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		// TODO Auto-generated method stub
		if(hasFocus){
			if(v==et_healthtest_abnormal_1){
				rb_healthtest_abnormal_no.setChecked(false);
				rb_healthtest_abnormal_yes.setChecked(true);
			}
			else if(v==et_lose_weight_goal){
				cb_hazards_control_5.setChecked(true);
			}
			else if(v==et_advical_vaccine){
				cb_hazards_control_6.setChecked(true);
			}
			else if(v==et_hazards_control_other){
				cb_hazards_control_7.setChecked(true);
			}
			else if(v==et_medicine_obedience_1){
				MyDialog(1).show();
			}
			else if(v==et_medicine_obedience_2){
				MyDialog(2).show();
			}
			else if(v==et_medicine_obedience_3){
				MyDialog(3).show();
			}
			else if(v==et_medicine_obedience_4){
				MyDialog(4).show();
			}
			else if(v==et_medicine_obedience_5){
				MyDialog(5).show();
			}
			else if(v==et_medicine_obedience_6){
				MyDialog(6).show();
			}
		}
		else if(v==et_vaccine_date_1&&hasFocus){
				mPopupWindow = new MyPopupWindow(getContext());			
				mPopupWindow.showAtLocation(findViewById(R.id.ht6_vaccine_date_1), Gravity.CENTER, 0, 0);

				Button btn=mPopupWindow.getButton();
				btn.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						et_vaccine_date_1.setText(mPopupWindow.getDate());
						mPopupWindow.dismiss();
					}
					
				});
			}
		else if(v==et_vaccine_date_2&&hasFocus){
			mPopupWindow = new MyPopupWindow(getContext());			
			mPopupWindow.showAtLocation(findViewById(R.id.ht6_vaccine_date_2), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_vaccine_date_2.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}				
			});
		}
		else if(v==et_vaccine_date_2&&hasFocus){
			mPopupWindow = new MyPopupWindow(getContext());			
			mPopupWindow.showAtLocation(findViewById(R.id.ht6_vaccine_date_3), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_vaccine_date_3.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}				
			});
		}
	}
}
